const Common = require('../../../utils/common.js');
import create from '../../../utils/create2.js'
import store from '../../../store/index.js'
import SessionUtil, { JxbHttpClient } from '../../../data/session/session.js';
create(store, {
  data: {
    optiond: {},
    itemAttribute: {},
    categoryId: '',
    flavorId: '',
    purposeId: '',
    priceRangeId: '',
    categoryId_checked: {},
    flavorId_checked: {},
    purposeId_checked: {},
    priceRangeId_checked: {},
    categoryName: "",
    rangeName: "",
    purposeName: "",
    flavorName: "",
    displayflavor: false,
    from: ''
  },
  onLoad: function (options) {
    SessionUtil.getInstance().then(session=>{
      let {from}=options, Filter = {}
      if (from && from * 1 === 1) { //从首页的专业推荐等模块进入
        Filter = this.store.data.goodsFilter
      } else {
        Filter = wx.getStorageSync('Filter');
      }
      if (Filter) {
        this.setData({
          categoryId: Filter.categoryId || '',
          flavorId: Filter.flavorId || '',
          purposeId: Filter.purposeId || '',
          priceRangeId: Filter.priceRangeId || '',
          categoryId_checked: this.getChecked(Filter.categoryId || ''),
          flavorId_checked: this.getChecked(Filter.flavorId || ''),
          purposeId_checked: this.getChecked(Filter.purposeId || ''),
          priceRangeId: parseInt(Filter.priceRangeId || '')
        });
      }
      if(typeof from !== 'undefined') this.setData({ from })
      wx.setNavigationBarTitle({
        title: '商品筛选'
      })
      this.itemAttribute();
      let pages = getCurrentPages();
      if (pages.length > 2) {
        let prevPage = pages[pages.length - 2]; //上两个页面
        prevPage.setData({
          tagValues: 0
        })
      }
      wx.setStorageSync("if_refresh", 4);
    })
  },
  getChecked: function (ID) {
    var ID_checked = {};
    if (ID.indexOf(",") > 0) {
      var IdArray = ID.split(",");
      for (var i = 0; i < IdArray.length; i++) {
        ID_checked[IdArray[i]] = 'true';
      }
    } else {
      ID = parseInt(ID);
      ID_checked[ID] = 'true';
    }
    return ID_checked;
  },

  itemAttribute: function () {
    // 产品筛选
    wx.showLoading({
      title: '数据加载中',
    });
    (new JxbHttpClient()).get('/AppMaster/Item/itemAttribute').then(jxbResp=>{
      let Attr = jxbResp.data;
      if (!Attr) {
        Common.Alert('错误提示', jxbResp.message);
        wx.hideLoading();
        return;
      }
      this.setData({
        itemAttribute: Attr
      });
      this.selectedcategory();
      this.selectedpriceRange();
      this.selectedpurpose();
      this.selectedflavor();
      // 关闭加载
      wx.hideLoading();
    })
  },
  upFilter_category: function (e) {
    // 类型（可多选）
    var categoryId = this.data.categoryId;
    var id = e.currentTarget.id;
    var categoryId_checked = {};
    if (id) {
      if (!categoryId) {
        categoryId = id;
        categoryId_checked[id] = 'true';
      } else {
        var IdArray = categoryId.split(",");
        if (Common.contains(IdArray, id)) {
          Common.removeByValue(IdArray, id);
        } else {
          IdArray.push(id);
        }
        for (var i = 0; i < IdArray.length; i++) {
          categoryId_checked[IdArray[i]] = 'true';
        }
        categoryId = IdArray.toString();
      }
      this.setData({
        categoryId: categoryId,
        categoryId_checked: categoryId_checked
      });
      this.selectedcategory()
    }
  },
  selectedcategory: function () {
    var page = this
    var categoryId = page.data.categoryId
    var a = categoryId.split(",");// 在每个逗号(,)处进行分解。
    var b = page.data.itemAttribute.category
    var c = '';
    for (var i = 0; i < b.length; i++) {
      if (Common.contains(a, b[i].category_id)) {
        c = c + b[i].category_name + ','
      }
    }
    if (Common.contains(a, 26)) {
      page.setData({
        displayflavor: true,
      });
    }
    c = c.substr(0, c.length - 1);
    page.setData({
      categoryName: c == '' ? '全部' : c,
    });
  },
  upFilter_price_range: function (e) {
    // 价格区间（单选）
    var priceRangeId = this.data.priceRangeId;
    var id = e.currentTarget.id;
    if (id) {
      if (priceRangeId == id) {
        id = ''
      }
      this.setData({
        priceRangeId: id
      });
      this.selectedpriceRange();
    }
  },
  selectedpriceRange: function () {
    var id = this.data.priceRangeId
    var b = this.data.itemAttribute.price_range
    var c = '';
    for (var i = 0; i < b.length; i++) {
      if (b[i].range_id == id) {
        c = b[i].range_name
      }
    }
    this.setData({
      rangeName: c == '' ? '全部' : c,
    });
  },
  upFilter_purpose: function (e) {
    // 用途（可多选）
    var purposeId = this.data.purposeId;
    var id = e.currentTarget.id;
    var purposeId_checked = {};
    if (id) {
      if (!purposeId) {
        purposeId = id;
        purposeId_checked[id] = 'true';
      } else {
        var IdArray = purposeId.split(",");
        if (Common.contains(IdArray, id)) {
          Common.removeByValue(IdArray, id);
        } else {
          IdArray.push(id);
        }
        for (var i = 0; i < IdArray.length; i++) {
          purposeId_checked[IdArray[i]] = 'true';
        }
        purposeId = IdArray.toString();
      }
      this.setData({
        purposeId: purposeId,
        purposeId_checked: purposeId_checked
      });
      this.selectedpurpose();
    }
  },

  selectedpurpose: function () {
    var purposeId = this.data.purposeId;
    var a = purposeId.split(",");// 在每个逗号(,)处进行分解。
    var b = this.data.itemAttribute.purpose
    var c = '';
    for (var i = 0; i < b.length; i++) {
      if (Common.contains(a, b[i].purpose_id)) {
        c = c + b[i].purpose_name + ','
      }
    }
    c = c.substr(0, c.length - 1);
    this.setData({
      purposeName: c == '' ? '全部' : c,
    });
  },
  upFilter_flavor: function (e) {
    // 香型（可多选）
    var flavorId = this.data.flavorId;
    var id = e.currentTarget.id;
    var flavorId_checked = {};
    if (id) {
      if (!flavorId) {
        flavorId = id;
        flavorId_checked[id] = 'true';
      } else {
        var IdArray = flavorId.split(",");
        if (Common.contains(IdArray, id)) {
          Common.removeByValue(IdArray, id);
        } else {
          IdArray.push(id);
        }
        for (var i = 0; i < IdArray.length; i++) {
          flavorId_checked[IdArray[i]] = 'true';
        }
        flavorId = IdArray.toString();
      }
      this.setData({
        flavorId: flavorId,
        flavorId_checked: flavorId_checked
      });
      this.selectedflavor();
    }
  },
  selectedflavor: function () {
    var flavorId = this.data.flavorId;
    var a = flavorId.split(",");// 在每个逗号(,)处进行分解。
    var b = this.data.itemAttribute.flavor
    var c = '';
    for (var i = 0; i < b.length; i++) {
      if (Common.contains(a, b[i].flavor_id)) {
        c = c + b[i].flavor_name + ','
      }
    }
    c = c.substr(0, c.length - 1);
    this.setData({
      flavorName: c == '' ? '全部' : c,
    });
  },
  onPullDownRefresh: function () {
    wx.stopPullDownRefresh()
  },
  Cancel: function () {
    var Filter = {};
    Filter.categoryId = '',
      Filter.flavorId = '',
      Filter.purposeId = '',
      Filter.priceRangeId = '',
      wx.setStorageSync('Filter', Filter);
    var pages = getCurrentPages();
    var prevPage = pages[pages.length - 2]; //上一个页面
    prevPage.setData({
      categoryId: '',
      flavorId: '',
      purposeId: '',
      priceRangeId: '',
      has_Filter: false,
      has_Filters: '../../images/sp_07.png',
      tagValues: 0,
      hasfilter: false
    })
    wx.setStorageSync('if_refresh', 2);
    wx.navigateBack();
  },
  Filter: function () {
    var page = this;
    var has_Filter = true,
      has_Filters = '';
    var categoryId = page.data.categoryId;
    var flavorId = page.data.flavorId;
    var purposeId = page.data.purposeId;
    var priceRangeId = page.data.priceRangeId;
    var Filter = {};
    Filter.categoryId = categoryId,
      Filter.flavorId = flavorId,
      Filter.purposeId = purposeId,
      Filter.priceRangeId = priceRangeId
    wx.setStorageSync('Filter', Filter);

    var pages = getCurrentPages();
    var prevPage = pages[pages.length - 2]; //上一个页面
    if (categoryId || flavorId || purposeId || priceRangeId) {
      has_Filter = true
      has_Filters = '../../images/sp_08.png'
    } else {
      has_Filter = false
      has_Filters = '../../images/sp_07.png'
    }
    const { from } = this.data
    if (from && from * 1 === 1) {
      const params = {
        categoryId: categoryId,
        flavorId: flavorId,
        purposeId: purposeId,
        priceRangeId: priceRangeId,
      }
      prevPage.setData({
        params,
        pageNo:1
      })
      prevPage.ItemList('Refresh')
      this.store.data.goodsFilter = params
      this.update()
    } else {
      prevPage.setData({
        categoryId: categoryId,
        flavorId: flavorId,
        purposeId: purposeId,
        priceRangeId: priceRangeId,
        has_Filter: has_Filter,
        has_Filters: has_Filters,
        tagValues: 0,
        hasfilter: false
      })
      wx.setStorageSync('if_refresh', 2);
    }
    wx.navigateBack();
  }
})